package xiaodong.train;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class Config {
	private static Properties prop = new Properties();

	static {
		FileInputStream fis = null;
		try {
			File file = new File("proxy.txt");
			if (!file.exists()) {
				file.createNewFile();
				BufferedWriter output = new BufferedWriter(new FileWriter(file));
				output.write("#代理服务器配置\r\n");
				output.write("#proxyset 为 true 时使用代理\r\n");
				output.write("proxyset=false\r\n");
				output.write("#proxyhost 为代理服务器IP\r\n");
				output.write("proxyhost=localhost\r\n");
				output.write("#proxyport 为代理服务器端口\r\n");
				output.write("proxyport=8080\r\n");
				output.close();
			}
			fis = new FileInputStream("proxy.txt");
			InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
			prop.load(isr);
			isr.close();
		} catch (IOException e) {
			System.err.println("加载配置文件出错  " + e);
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static String getProperty(String key) {
		return prop.getProperty(key, "");
	}

	public static boolean isUseProxy() {
		return "true".equals(getProperty("proxyset"));
	}

	public static String getProxyIp() {
		return getProperty("proxyhost");
	}

	public static int getProxyPort() {
		return Integer.parseInt(getProperty("proxyport"));
	}

	public static void main(String[] args) {
		System.out.println(isUseProxy());
		System.out.println(getProxyIp());
		System.out.println(getProxyPort());
	}
}
